package com.cd826dong.clouddemo.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@RestController()
@RequestMapping("/rabbionClientApi")
public class RabbionClientApi {

    @Autowired
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String restTemplateTest(){
        ServiceInstance instance = loadBalancerClient.choose("HP-SERVICE");
        String uri = String.format("http://%s:%s/hello/{name}", instance.getHost(), instance.getPort());
        return restTemplate.getForEntity(uri, String.class, "loadBalancerClient").getBody();
    }
}
